package com.qdxwx.data.service.coupon;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.coupon.CouponCodeMapper;
import com.qdxwx.data.mapper.coupon.CouponMapper;
import com.qdxwx.models.coupon.Coupon;
import com.qdxwx.models.coupon.CouponLimit;
import com.qdxwx.models.course.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Coupon接口
 * @author developer
 */
@Service
public class CouponServiceImpl implements CouponService {

 	@Autowired
    private CouponMapper couponMapper;
 	@Autowired
    private CouponCodeMapper couponCodeMapper;

    /**
     * 添加Coupon
     * @param coupon 要添加的Coupon
     * @return id
     */
    @Override
    public int insert(Coupon coupon){
        coupon.setCreateTime(LocalDateTime.now());
    	return couponMapper.insert(coupon);
    }

    /**
     * 根据id删除一个Coupon
     * @param id 要删除的id
     */
    @Override
    public int deleteById(long id){
        if(id < 1) return 0;
    	int row = couponMapper.deleteById(id);
        if(row > 0) couponCodeMapper.deleteCouponCodeByCouponId(id);
        return row;
    }

    /**
     * 修改Coupon使用数量
     */
    @Override
    public void updateUseNum(Long id){
    	couponMapper.updateUseNum(id);
    }
    /**
     * 根据id获取单个Coupon对象
     * @param id 要查询的id
     * @return Coupon
     */
    @Override
    public Coupon get(Long id){
    	return couponMapper.selectById( id);
    }

    /**
     * 优惠券课程限制
     */
    @Override
    public void addLimitCourse(List<CouponLimit> couponLimits) {
        couponMapper.addCouponLimitCourse(couponLimits);

    }

    /**
     * 优惠券id查找限制课程
     * @param id
     * @return
     */
    @Override
    public List<Course> getLimitCourseById(Long id){
        return couponMapper.selectLimitCourseById(id);
    }

    @Override
    public List<Coupon> getList(Coupon coupon, PageRequest page) {
        return couponMapper.selectList(coupon,page);
    }

    @Override
    public long count(Coupon coupon) {
        return couponMapper.count(coupon);
    }

}